﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;

using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;

using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;

using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.NganHang.NH_GiayCo
{
    public partial class NH_GiayCo_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "GiayCo"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public NH_GiayCo_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public NH_GiayCo_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.NganHang.NH_GiayCo.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.NH_GiayCo_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.NH_GiayCo_MoreInformation_GroupBox, controls);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource NH_GiayCo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["NH_GiayCo_ViewModelSource"];
                if (Khoa == null)
                {
                    NH_GiayCo_ViewModel NH_GiayCo_ViewModel = new NH_GiayCo_ViewModel();
                    NH_GiayCo_ViewModel.ChildList_AddedNew += new NH_GiayCo_ViewModel.ChildList_AddedNew_EventHandler(NH_GiayCo_ViewModel_ChildList_AddedNew);
                    
                    NH_GiayCo_ViewModel.TCHangHoaChanged += new EventHandler(NH_GiayCo_TinhChatHangHoaChanged);
                    NH_GiayCo_ViewModel.hangHoaIdChanged += new NH_GiayCo_ViewModel.HangHoaIdChanged_EventHandler(NH_GiayCo_HangHoaIdChanged);
                   
                    NH_GiayCo_ViewModel.LoaiTienTeChanged += new EventHandler(NH_GiayCo_LoaiTienTeChanged);

                    NH_GiayCo_ViewModel.ThueSuatIdChanged += new NH_GiayCo_ViewModel.ThueSuatIdChanged_EventHandler(NH_GiayCo_ThueSuatIdChanged);

                    NH_GiayCo_CollectionViewSource.Source = new List<object> { NH_GiayCo_ViewModel };

                    
                }
                else
                {
                    NH_GiayCo_ViewModel NH_GiayCo_ViewModel = new NH_GiayCo_ViewModel((int)Khoa);
                    NH_GiayCo_ViewModel.ChildList_AddedNew += new NH_GiayCo_ViewModel.ChildList_AddedNew_EventHandler(NH_GiayCo_ViewModel_ChildList_AddedNew);
                    NH_GiayCo_ViewModel.TCHangHoaChanged += new EventHandler(NH_GiayCo_TinhChatHangHoaChanged);
                    NH_GiayCo_ViewModel.hangHoaIdChanged += new NH_GiayCo_ViewModel.HangHoaIdChanged_EventHandler(NH_GiayCo_HangHoaIdChanged);
                    NH_GiayCo_ViewModel.LoaiTienTeChanged += new EventHandler(NH_GiayCo_LoaiTienTeChanged);
                    
                    NH_GiayCo_ViewModel.ThueSuatIdChanged += new NH_GiayCo_ViewModel.ThueSuatIdChanged_EventHandler(NH_GiayCo_ThueSuatIdChanged);

                    NH_GiayCo_CollectionViewSource.Source = new List<object> { NH_GiayCo_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };
                

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

              
               
                DoFilterOnDefaultDM_SanPham();

                this.NH_GiayCo_GiayCoChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.NH_GiayCo_GiayCoChiTietGridView);
                this.NH_GiayCo_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.NH_GiayCo_ThueChiTietGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.NH_GiayCo_GiayCoChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.NH_GiayCo_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.NH_GiayCo_GiayCoChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.NH_GiayCo_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
               
            }
        }

        #endregion
       

        #region Event Handle
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                NH_GiayCo_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.NH_GiayCo_GiayCoChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.NH_GiayCo_ThueChiTietTab.IsSelected = true;
                }
               
                
            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.NH_GiayCo_GiayCoChiTietTab.IsSelected)
            {
                AddGiayCoDetailsItem();
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.NH_GiayCo_GiayCoChiTietTab.IsSelected)
            {
                return NH_GiayCo_GiayCoChiTietGridView;
            }
            else if (this.NH_GiayCo_ThueChiTietTab.IsSelected)
            {
                return NH_GiayCo_ThueChiTietGridView;
            }
  
            return null;
        }



        void DeleteItems()
        {
            if (this.NH_GiayCo_GiayCoChiTietTab.IsSelected)
            {
                NH_GiayCo_ChiTietChild ChiTietChild = (NH_GiayCo_ChiTietChild)(this.NH_GiayCo_GiayCoChiTietGridView.SelectedItem);
                IList<NH_GiayCo_ChiTietChild> list = GetViewModel().Model.NH_GiayCo_ChiTiets;
                int index = list.IndexOf(ChiTietChild);
                
                DeleteGiayCoDetailsItem(index);
            }
           
            
        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }
              
        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           NH_GiayCo_ViewModel vm = GetViewModel();
            vm.SaveAndNew();
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGiayCoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGiayCoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    this.AddGiayCoDetailsItem();
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }

        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            this.tabControl.SelectedItem = tabControl.Items[0];
            this.AddGiayCoDetailsItem();
        }


        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {
 
            List<TabItem> AvailableTabItem = new List<TabItem>();
            int indexCurrentTab = 0;
            foreach (TabItem item in this.tabControl.Items)
            {
                if(item.Visibility == System.Windows.Visibility.Visible)
                {
                    AvailableTabItem.Add(item);
                    if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                    {
                        indexCurrentTab = AvailableTabItem.IndexOf(item);
                    }
                    
                }
            }

            
            TabItem[] list = AvailableTabItem.ToArray();
            if (indexCurrentTab == (AvailableTabItem.Count - 1))
            {
                this.tabControl.SelectedItem = list[0];
                FocusAfterChangeTab(this.NH_GiayCo_GiayCoChiTietGridView, e.Index);
                
            }
            else
            {
                this.tabControl.SelectedItem = list[indexCurrentTab + 1];
                if (list[indexCurrentTab + 1].Name.Equals("NH_GiayCo_ThueChiTietTab"))
                {
                    FocusAfterChangeTab(this.NH_GiayCo_ThueChiTietGridView,e.Index);
                }
            }
            
        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
                
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
                
           
        }
        private void NH_GiayCo_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        

        private void NH_GiayCo_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

       
        #endregion

        #region View Model Related Methods
        private NH_GiayCo_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource NH_GiayCo_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["NH_GiayCo_ViewModelSource"];
            return ((List<object>)NH_GiayCo_CollectionViewSource.Source)[0] as NH_GiayCo_ViewModel;
        }

        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
        }

        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

        public void DoFilterOnDefaultDM_SanPham()
        {
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
            dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };
           
        }
     

        void NH_GiayCo_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;    
            
            NH_GiayCo_ThueChild ThueChild = GetViewModel().Model.NH_GiayCo_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = (double) item.GiaTriTinhThue;
                }
            }

           

        }
        void NH_GiayCo_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
              
           
        }

        void NH_GiayCo_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
       
        
        void NH_GiayCo_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            /*NH_GiayCo_ChiTietChild ChiTietChild =  GetViewModel().Model.NH_GiayCo_ChiTiets[e.Index];
            NH_GiayCo_ThueChild ThueChild =  GetViewModel().Model.NH_GiayCo_Thues[e.Index];
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.SoTien = (double) item.DonGiaBan;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.ThueSuatId = item.ThueSuatId;    
                    break;
                }
            }*/
            

     
        }
     

        private void AddThueDetailsItem()
        {
            GetViewModel().AddNH_GiayCo_ThueItem();
        }

        private void AddGiayCoDetailsItem()
        {
            GetViewModel().AddNH_GiayCo_ChiTietItem();
            
        }

       
        

        private void DeleteThueDetailsItem(int Index)
        {
            GetViewModel().DeleteNH_GiayCo_ThueItem(Index);
        }

        private void DeleteGiayCoDetailsItem(int Index)
        {
            GetViewModel().DeleteNH_GiayCo_ChiTietItem( Index);
        }

       
        

        void NH_GiayCo_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "NH_GiayCo_ChiTiets")
            {
                this.NH_GiayCo_GiayCoChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "NH_GiayCo_Thues")
            {
                this.NH_GiayCo_ThueChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            
        }

       
        #endregion
        
    }
}
